package compiler_rt;

import java.util.Vector;

/**
 * The class extended by all constructors Any constructor should provide it's
 * name and it's number.
 * 
 * Some predefined constructors such as those of Nat and Bool may override the
 * toString function.
 */
public class Constructor extends Closure {
	private static final long serialVersionUID = 3402141065784768294L;
	private Vector<Closure> params = new Vector<Closure>(4);
	private int no = 0;
	private String name = null;

	/* Constructor number */
	public Constructor(int no, String name) {
		super(null, null);
		this.no = no;
		this.name = name;
	}

	public int getNo() {
		return no;
	}

	/* Constructor application only stores parameters inside the constructor */
	@Override
	public Closure app(Closure param) {
		Constructor activation = (Constructor) this.activate();
		if(param != null) // it is not a type argument
			activation.params.add(param);
		return activation;
	}

	/* 'case' analisys applies the selected function to accumulated parameters */
	public Closure case_of(Closure fs) {
		Closure crt = fs;
		for (Closure c : params) {
			crt = crt.app(c);
		}
		return crt;
	}

	public String toString() {
		if (params.isEmpty())
			return name;
		else
			return name + "" + params.toString();
	}

	@Override
	public Closure activate() {
		Constructor ret = (Constructor) this.clone();
		ret.params = new Vector<Closure>(this.params);
		return ret;
	}
}
